package com.blb.community_admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.SysLogininfor;
import com.blb.community_admin.service.ILogininforService;
import com.blb.community_admin.util.EasyExcelUtil;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 登录日志的控制器
 *
 * @author hkp
 */
@RestController
public class LogininforController {

    @Resource
    private ILogininforService logininforService;

    /**
     * 分页模糊查询
     *
     * @param current
     * @param size
     * @param ipaddr
     * @param userName
     * @param status
     * @param loginTime
     * @return
     */
    @GetMapping("/logininfor/page")
    public ResponseResult<IPage<SysLogininfor>> selectLoginInForPageByFuzzy(Integer current, Integer size, String ipaddr, String userName, String status, String loginTime, String prop, String order) {
        /**
         * @param startTime 开始时间
         * @param endTime 结束时间
         */
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotEmpty(loginTime)) {
            String[] split = loginTime.split(",");
            startTime = split[0];
            endTime = split[1];
        }
        if ("ascending".equals(order)) {
            order = "ASC";
        } else if ("descending".equals(order)) {
            order = "DESC";
        }
        if (StringUtils.isNotEmpty(prop)) {
            if ("userName".equals(prop)) {
                prop = "user_name";
            } else {
                prop = "login_time";
            }
        }
        IPage<SysLogininfor> pages = logininforService.selectLoginInForPage(current, size, ipaddr, userName, status, startTime, endTime, prop, order);
        return ResponseResult.ok(pages);
    }

    /**
     * 清空所有登录日志
     *
     * @return
     */
    @Log(module = "登录日志", operationType = 9, requestMethod = "DELETE")
    @DeleteMapping("/logininfor/all")
    public ResponseResult<String> deleteAll() {
//        查询所有数据
        List<SysLogininfor> list = logininforService.list(null);
//        映射成infoId的集合
        List<Long> infoIdList = list.stream().map(SysLogininfor::getInfoId).collect(Collectors.toList());
        logininforService.removeByIds(infoIdList);
        return ResponseResult.ok("ok");
    }

    /**
     * 批量删除
     *
     * @return
     */
    @DeleteMapping("/logininfor/delete")
    public ResponseResult<String> deleteByIds(@RequestBody List<SysLogininfor> selections) {
        List<Long> infoIdList = selections.stream().map(SysLogininfor::getInfoId).collect(Collectors.toList());
        logininforService.removeByIds(infoIdList);
        return ResponseResult.ok("ok");
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/logininfor")
    public ResponseResult<List<SysLogininfor>> selectAll() {
        List<SysLogininfor> list = logininforService.list(null).stream().distinct().collect(Collectors.toList());
        return ResponseResult.ok(list);
    }

    /**
     * 导出
     *
     * @param sysLoginInFors
     * @return
     */
    @ApiOperation("导出")
    @PostMapping("/logininfor/export")
    public ResponseEntity<ByteArrayResource> export(@RequestBody List<SysLogininfor> sysLoginInFors) throws IOException {
        String path = EasyExcelUtil.write("登录日志", SysLogininfor.class, sysLoginInFors);
        ResponseEntity<ByteArrayResource> download = EasyExcelUtil.download(path);
        return download;
    }
}
